<?php 
class kdtrace_aramex extends Controller {
	
	function track( $tracknum , $lang = ""){
		
		$url 	 = "http://www.aramex.com/track-results-multiple.aspx?ShipmentNumber=" . $tracknum ;
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
			'Referer: http://www.aramex.com/',
			'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36' 
		));
		$result = curl_exec($ch);
        curl_close($ch);
		$rtndata = array('tracknum'=>$tracknum,'header'=>array(),'err'=>array(),'eventList'=>array());

		if( strpos($result , "data not currently available")>0 ){
			$rtndata['err']['errcode'] = '1';
			return $rtndata;
		}
		$result = $this->skipSpaces($result);
		$rtndata['err']['errcode'] = '0';

		if(preg_match("/id\=\"ctl00_ctl00_MainContent_InnerMainContent_hdnParams\" value\=\"(.+?)\"/i", $result, $matches) < 1){
			$rtndata['err']['errcode'] = '2';
			$rtndata['err']['errmsg']  = 'program error';
			return $rtndata;
		};
		$matches = explode("|",$matches[1]);
		
		$getdata = '{"AWBID": "' . $matches[0] . '","ProductGroup": "' . $matches[1] . '","Language": "' . $matches[2] . '","IsLoggedIn": "False","SignatureInnerHtml": "(view attached signature)","SelectedAccount": "","SearchMode": "0","SearchCriteria": ""}';
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, "http://www.aramex.com/WebServices/JQuery-Post-Handler.asmx/GetExpressHAWBTrackingResults");
        curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $getdata);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
			'Content-Type: application/json; charset=UTF-8',
			'Referer: http://www.aramex.com/track-results-multiple.aspx',
			'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36' 
		));
		$result = curl_exec($ch);
        curl_close($ch);
		$result = json_decode($result)->d;
		$result = json_decode($result);
		$package = $result->TrackingResult->HAWBEntity;
			$rtndata['header']['shipFrom']			= $package->OriginCountry;
			$rtndata['header']['shipTo']			= $package->DestinationCountry;
			$rtndata['header']['shipFromCity']		= $package->OriginCity;
			$rtndata['header']['shipToCity']		= $package->DestinationCity;
			
			$rtndata['header']['DetailStatus']		= $package->CurrentStatus;

			$package->DeliveredDate !="" ? $rtndata['header']['isDelivered'] = true : $rtndata['header']['isDelivered'] = false;
		$tckresult = $result->TrackingResult->HAWBHistoryEntity;
		$i = 0;
		foreach( $tckresult as $tlist){
			$tt =  strtotime($tlist->ActionDate);
			$rtndata['eventList'][$i] = array(
				"date"			=> date('Y-m-d',$tt),
				"time"			=> date('h:i:s',$tt),
				"utime"			=> $tt,
				"status"		=> $tlist->CustomerDescription,
				"location"		=> $tlist->UpdateLocationFormatted
			);
			$i++;
		}

		$rtndata['eventList'] = array_reverse( $rtndata['eventList'] );

		return $rtndata;
	}
		function skipSpaces($t){
			return  trim(preg_replace('/\s{2,}/', ' ', $t));
	}

}

?>